package connection {
import flash.events.EventDispatcher;
import flash.geom.Point;

import geditor.common.AlignInformation;
import geditor.common.DrawPoint;

public class ConnectionDrawPoint extends EventDispatcher implements DrawPoint {
    private var _connection:ConnectionComponent;
    private var _start:Boolean;

    public function ConnectionDrawPoint(connection:ConnectionComponent, start:Boolean) {
        _connection = connection;
        _start = start;
    }

    public function get point():Point {
        return connectionPoint.point;
    }

    public function canAlign(obj:Object):AlignInformation {
        if (obj is ConnectionPoint) {
            var cp:ConnectionPoint = ConnectionPoint(obj);
            if (cp.canConnect(_connection, _start)) {
                var curPoint:Point = point;
                var distance:Number = Point.distance(curPoint, cp.point);
                return new AlignInformation(distance, cp.point, cp);
            }
        }

        return null;
    }

    public function doAlign(align:Object):void {
        if (align is ConnectionPoint) {
            connectionPoint = ConnectionPoint(align);
            _isTemp = false;
        }
    }

    private function set connectionPoint(cp:ConnectionPoint):void {
        if (_start) {
            _connection.start = cp;
        } else {
            _connection.end = cp;
        }
    }

    private function get connectionPoint():ConnectionPoint {
        if (_start) {
            return _connection.start;
        } else {
            return _connection.end;
        }
    }

    private var _temp:TempConnectionPoint = new TempConnectionPoint();
    private var _isTemp:Boolean;

    public function set temp(value:Point):void {
        _temp.p = value;
        connectionPoint = _temp;
        _isTemp = true;
    }

    public function get isTemp():Boolean {
        return _isTemp;
    }
}
}